package cn.yunyichina.reservation.controller.business;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.common.ResponseEnum;
import cn.yunyichina.reservation.common.jackson.Jackson;
import cn.yunyichina.reservation.constant.Console;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.constant.Interface;
import cn.yunyichina.reservation.controller.BaseController;
import cn.yunyichina.reservation.util.RequestUtil;
import cn.yunyichina.reservation.util.UUIDUtil;
import cn.yunyichina.reservation.vo.ConsoleUserVo;
import cn.yunyichina.reservation.vo.DeptVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lullaby on 2016/8/29.
 */
@RestController
@RequestMapping("console")
public class DeptController extends BaseController {

    private static final String RESERVATION_CONSOLE_DEPT_LIST = "yyt.reservation.dept.list";
    private static final String RESERVATION_CONSOLE_DEPT_CREATE = "yyt.reservation.dept.create";
    private static final String RESERVATION_CONSOLE_DEPT_GET = "yyt.reservation.dept.get";
    private static final String RESERVATION_CONSOLE_DEPT_UPDATE = "yyt.reservation.dept.update";
    private static final String RESERVATION_CONSOLE_DEPT_DELETE = "yyt.reservation.dept.delete";

    @GetMapping("depts")
    public HttpResponse listDepts(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = RequestUtil.getAllParameters(request);
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_DEPT_LIST, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            List<DeptVo> depts = Jackson.getBeanJsonData(serviceResponse.getResult(), List.class);
            httpResponse.setData(depts);
        } else {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

    @PostMapping("dept")
    public HttpResponse createDept(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = new HashMap<>();
        ConsoleUserVo user = getConsoleUser(request.getAttribute(Console.USERNAME).toString());
        params.put("id", UUIDUtil.get32UUID());
        params.put("mechanism_id", request.getParameter("mechanism_id"));
        params.put("name", request.getParameter("name"));
        params.put("code", request.getParameter("code"));
        params.put("address", request.getParameter("address"));
        params.put("create_user", user.getUsername());
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_DEPT_CREATE, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (!Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

    @GetMapping("dept")
    public HttpResponse getDept(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        String id = request.getParameter("id");
        Map<String, String> params = new HashMap<>();
        params.put("id", id);
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_DEPT_GET, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.setData(Jackson.getBeanJsonData(serviceResponse.getResult(), DeptVo.class));
        } else {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

    @PutMapping("dept")
    public HttpResponse updateDept(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = new HashMap<>();
        params.put("id", request.getParameter("id"));
        params.put("name", request.getParameter("name"));
        params.put("code", request.getParameter("code"));
        params.put("address", request.getParameter("address"));
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_DEPT_UPDATE, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (!Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

    @DeleteMapping("dept")
    public HttpResponse deleteDept(HttpServletRequest request) {
        HttpResponse httpResponse = new HttpResponse();
        Map<String, String> params = new HashMap<>();
        String id = request.getParameter("id");
        params.put("id", id);
        Response serviceResponse = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_DEPT_DELETE, Jackson.getBaseJsonData(params)
        );
        if (serviceResponse == null) {
            return httpResponse.failure(ResponseEnum.FAILURE.getCode(), ResponseEnum.FAILURE.getMessage());
        }
        if (!Interface.SUCCESS.equals(serviceResponse.getResultCode())) {
            httpResponse.failure(serviceResponse.getResultCode(), serviceResponse.getResultMessage());
        }
        return httpResponse;
    }

}
